<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : AudioSample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">AudioSample</p>
    	
       <p class="linkListHeader">Fields</p>
       <p class="linkList">
           
    <a href="audiosample_field_left.html" title="int used to request the left channel of audio from the getChannel method.">LEFT</a><br/>
    
    <a href="audiosample_field_right.html" title="int used to request the right channel of audio from the getChannel method.">RIGHT</a><br/>
    
    <a href="audiosample_field_left.html" title="The AudioBuffer containing the left channel samples. If this is a mono
 sound, it contains the single channel of audio.">left</a><br/>
    
    <a href="audiosample_field_mix.html" title="The AudioBuffer containing the mix of the left and right channels. If this is
 a mono sound, <code>mix</code> contains the same
 samples as <code>left</code>.">mix</a><br/>
    
    <a href="audiosample_field_right.html" title="The AudioBuffer containing the right channel samples. If this is a mono
 sound, <code>right</code> contains the same samples as
 <code>left</code>.">right</a><br/>
    
	   </p>
   
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="audiosample_method_addlistener.html" title="Add an AudioListener to this sound generating object.">addListener ( )</a><br/>
    
    <a href="audiosample_method_buffersize.html" title="The internal buffer size of this sound object.">bufferSize ( )</a><br/>
    
    <a href="audiosample_method_getbalance.html" title="Returns the current balance.">getBalance ( )</a><br/>
    
    <a href="audiosample_method_getchannel.html" title="Gets the samples for the requested channel number as a float array. 
 Use either AudioSample.LEFT or AudioSample.RIGHT.">getChannel ( )</a><br/>
    
    <a href="audiosample_method_getformat.html" title="Returns AudioFormat object that describes the audio properties 
 of this sound generating object.">getFormat ( )</a><br/>
    
    <a href="audiosample_method_getgain.html" title="Returns the current gain.">getGain ( )</a><br/>
    
    <a href="audiosample_method_getmetadata.html" title="Get the AudioMetaData for this sample.">getMetaData ( )</a><br/>
    
    <a href="audiosample_method_getpan.html" title="Returns the current pan.">getPan ( )</a><br/>
    
    <a href="audiosample_method_getvolume.html" title="Returns the current volume.">getVolume ( )</a><br/>
    
    <a href="audiosample_method_ismuted.html" title="Returns true if the sound is muted.">isMuted ( )</a><br/>
    
    <a href="audiosample_method_length.html" title="Gets the length in milliseconds of this AudioSample.">length ( )</a><br/>
    
    <a href="audiosample_method_mute.html" title="Mutes the sound.">mute ( )</a><br/>
    
    <a href="audiosample_method_removelistener.html" title="Removes an AudioListener that was previously 
 added to this sound object.">removeListener ( )</a><br/>
    
    <a href="audiosample_method_samplerate.html" title="Returns the sample rate of this sound object.">sampleRate ( )</a><br/>
    
    <a href="audiosample_method_setbalance.html" title="Sets the balance.">setBalance ( )</a><br/>
    
    <a href="audiosample_method_setgain.html" title="Sets the gain.">setGain ( )</a><br/>
    
    <a href="audiosample_method_setpan.html" title="Sets the pan.">setPan ( )</a><br/>
    
    <a href="audiosample_method_setvolume.html" title="Sets the volume.">setVolume ( )</a><br/>
    
    <a href="audiosample_method_shiftbalance.html" title="Transitions the balance from one value to another.">shiftBalance ( )</a><br/>
    
    <a href="audiosample_method_shiftgain.html" title="Transitions the gain from one value to another.">shiftGain ( )</a><br/>
    
    <a href="audiosample_method_shiftpan.html" title="Transitions the pan from one value to another.">shiftPan ( )</a><br/>
    
    <a href="audiosample_method_shiftvolume.html" title="Transitions the volume from one value to another.">shiftVolume ( )</a><br/>
    
    <a href="audiosample_method_stop.html" title="Stops all sound being produced by this AudioSample.">stop ( )</a><br/>
    
    <a href="audiosample_method_trigger.html" title="Triggers the sound to play once. Can be called again before the sound
 finishes playing.">trigger ( )</a><br/>
    
    <a href="audiosample_method_type.html" title="The type is an int describing the number of channels
 this sound object has.">type ( )</a><br/>
    
    <a href="audiosample_method_unmute.html" title="Unmutes the sound.">unmute ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	An <code>AudioSample</code> keeps the entire file in an internal buffer and
 all you can do is <code>trigger()</code> the sound. However, you can trigger
 the sound even if it is still playing back. It is not advised that you use
 this class for long sounds (like entire songs, for example) because the
 entire file is kept in memory.
 <p>
 To create an AudioSample you must use either the loadSample or createSample 
 methods of the Minim class.
 <p>
 AudioSample also provides most of the same methods as AudioPlayer for
 controlling volume, panning, and so forth.
 <p>
 We now recommend using <code>Sampler</code> class from the ugens package because it is more
 full-featured than <code>AudioSample</code>.
    	<p class="memberSectionHeader">Constructors</p>
    	<pre></pre>
    	
    	<p class="memberSectionHeader">Related</p>
    	<A href="minim_class_minim.html">Minim</A><BR>

    	<p class="memberSectionHeader">Example</p>
    	<pre>/**
  * This sketch demonstrates how to use the &lt;code>loadSample&lt;/code> method of &lt;code>Minim&lt;/code>. 
  * The &lt;code>loadSample&lt;/code> method allows you to specify the sample you want to load 
  * with a &lt;code>String&lt;/code> and optionally specify what you want the buffer size of the 
  * returned &lt;code>AudioSample&lt;/code> to be. Minim is able to load wav files, au files, aif
  * files, snd files, and mp3 files. When you call &lt;code>loadSample&lt;/code>, if you just 
  * specify the filename it will try to load the sample from the data folder of your sketch. 
  * However, you can also specify an absolute path (such as "C:\foo\bar\thing.wav") and the 
  * file will be loaded from that location (keep in mind that won't work from an applet). 
  * You can also specify a URL (such as "http://www.mysite.com/mp3/song.mp3") but keep in mind 
  * that if you run the sketch as an applet you may run in to security restrictions 
  * if the applet is not on the same domain as the file you want to load. You can get around 
  * the restriction by signing all of the jars in the applet.
  * &lt;p>
  * An &lt;code>AudioSample&lt;/code> is a special kind of file playback that allows
  * you to repeatedly &lt;i>trigger&lt;/i> an audio file. It does this by keeping the
  * entire file in an internal buffer and then keeping a list of trigger points.
  * &lt;code>AudioSample&lt;/code> supports up to 20 overlapping triggers, which
  * should be plenty for short sounds. It is not advised that you use this class
  * for long sounds (like entire songs, for example) because the entire file is
  * kept in memory.
  * &lt;p>
  * Use 'k' and 's' to trigger a kick drum sample and a snare sample, respectively. 
  * You will see their waveforms drawn when they are played back.
  * &lt;p>
  * For more information about Minim and additional features, 
  * visit http://code.compartmental.net/minim/
  */

import ddf.minim.*;

Minim minim;
AudioSample kick;
AudioSample snare;

void setup()
{
  size(512, 200, P3D);
  minim = new Minim(this);

  // load BD.wav from the data folder
  kick = minim.loadSample( "BD.mp3", // filename
                            512      // buffer size
                         );
                         
  // An AudioSample will spawn its own audio processing Thread, 
  // and since audio processing works by generating one buffer 
  // of samples at a time, we can specify how big we want that
  // buffer to be in the call to loadSample. 
  // above, we requested a buffer size of 512 because 
  // this will make the triggering of the samples sound more responsive.
  // on some systems, this might be too small and the audio 
  // will sound corrupted, in that case, you can just increase
  // the buffer size.
  
  // if a file doesn't exist, loadSample will return null
  if ( kick == null ) println("Didn't get kick!");
  
  // load SD.wav from the data folder
  snare = minim.loadSample("SD.wav", 512);
  if ( snare == null ) println("Didn't get snare!");
}

void draw()
{
  background(0);
  stroke(255);
  
  // use the mix buffer to draw the waveforms.
  for (int i = 0; i &lt; kick.bufferSize() - 1; i++)
  {
    float x1 = map(i, 0, kick.bufferSize(), 0, width);
    float x2 = map(i+1, 0, kick.bufferSize(), 0, width);
    line(x1, 50 - kick.mix.get(i)*50, x2, 50 - kick.mix.get(i+1)*50);
    line(x1, 150 - snare.mix.get(i)*50, x2, 150 - snare.mix.get(i+1)*50);
  }
}

void keyPressed() 
{
  if ( key == 's' ) snare.trigger();
  if ( key == 'k' ) kick.trigger();
}

</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>